/**
  ******************************************************************************
  * @file    Remote_decode.c
  * @brief   ¶ÔÒ£¿ØÆ÷Êý¾Ý½øÐÐ½âÂë       
  ****************************************************************************
  */


#include "dt7.h"
#include "usart.h"
#include "can.h"

uint8_t USART1_DMA_RX_BUF[BSP_USART1_DMA_RX_BUF_LEN];  //¶¨ÒåÒ»¸öÊý×éÓÃÓÚ´æ·Å´ÓDMA½ÓÊÕµ½µÄÒ£¿ØÆ÷Êý¾Ý
 
/**
  * @brief :  ½ÓÊÕÒ£¿ØÆ÷Êý¾Ý
  * @param :  None
  * @retval:  None
  * @note  :  Î»ÓÚUSART1µÄÖÐ¶Ïº¯ÊýÖÐ
  */
  
void RemotreCtl_Data_Receive(void)
{
	uint32_t rx_data_len = 0;	                                                          //±¾´Î½ÓÊÕ³¤¶È
	if((__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE)!=RESET)) 
	{
		__HAL_UART_CLEAR_IDLEFLAG(&huart1);                                           //Çå³ý¿ÕÏÐÖÐ¶ÏµÄ±êÖ¾
		(void)USART1->SR;                                                             //Çå¿ÕSR¼Ä´æÆ÷
		(void)USART1->DR;                                                             //Çå¿ÕDR¼Ä´æÆ÷
		__HAL_DMA_CLEAR_FLAG(&hdma_usart1_rx,DMA_FLAG_TCIF2_6);                       //Çå³ý DMA2_Steam2´«ÊäÍê³É±êÖ¾
		HAL_UART_DMAStop(&huart1);                                                    //´«ÊäÍê³ÉÒÔºó¹Ø±Õ´®¿ÚDMA
		rx_data_len=BSP_USART1_DMA_RX_BUF_LEN-__HAL_DMA_GET_COUNTER(&hdma_usart1_rx); //»ñÈ¡ÕâÒ»´ÎÊý¾ÝÁ¿´óÐ¡£¨×Ü³¤¶È-±£ÁôµÄ³¤¶È£©
		HAL_UART_Receive_DMA(&huart1, USART1_DMA_RX_BUF, BSP_USART1_DMA_RX_BUF_LEN);  //½ÓÊÜÊý¾Ý
		if(rx_data_len == 18)                                                         //ÅÐ¶ÏÊý¾ÝÊÇ·ñÎªÕýÈ·µÄÊý¾Ý³¤¶È
		{
			RC_DataHandle(USART1_DMA_RX_BUF);                                           //½øÈëÊý¾Ý½âÂëº¯Êý
		}
	}
}

/**
  * @brief :  ¿ªÊ¼´®¿ÚÒ»²»¶¨³¤ÖÐ¶Ï
  * @param :  None
  * @retval:  None
  * @note  :  Î»ÓÚÖ÷º¯Êý³õÊ¼»¯ÖÐ
  */

void RemotreCtl_Data_Receive_Start(void)
{
	__HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE);                                       //¿ªÆô²»¶¨³¤ÖÐ¶Ï
}



RC_Ctl_t RC_CtrlData = {{0x400, 0x400, 0x400, 0x400}};       //Ò£¿ØÆ÷ÊäÈë

/**
  * @brief :  ¸ù¾ÝÒ£¿ØÆ÷Ð­Òé½øÐÐ½ÓÊÕµ½µÄÊý¾ÝµÄ´¦Àí
  * @param :  pData£ºÒ»¸öÖ¸Ïò8Î»Êý¾ÝµÄÖ¸Õë
  * @retval:  None
  * @note  :  None
  */

void RC_DataHandle(uint8_t *pData)
{
	if(pData == NULL)
	{
			return;
	}
	/*pData[0]ÎªRightXµÄµÍ8Î»£¬Data[1]µÄµÍ3Î»RightXµÄ¸ß3Î»*/
	RC_CtrlData.rc.RightX = ((uint16_t)pData[0] | (uint16_t)pData[1] << 8) & 0x07FF;

	/*pData[1]µÄ¸ß5Î»ÎªRightYµÄµÍ5Î»£¬pData[2]µÄµÍ6Î»ÎªRightYµÄ¸ß6Î»*/
	RC_CtrlData.rc.RightY = ((uint16_t)pData[1] >> 3 | (uint16_t)pData[2] << 5) & 0x07FF;

	/*pData[2]µÄ¸ß2Î»ÎªLeftXµÄµÍ2Î», pData[3]ÎªLeftXµÄÖÐ8Î»£¬pData[4]µÄµÍ1Î»ÎªLeftXµÄ¸ß1Î»*/
	RC_CtrlData.rc.LeftX = ((uint16_t)pData[2] >> 6 | (uint16_t)pData[3] << 2 | (uint16_t)pData[4] << 10) & 0x07FF;

	/*pData[4]µÄ¸ß7Î»ÎªLeftYµÄµÍ7Î»£¬pData[5]µÄµÍ4Î»ÎªLeftYµÄ¸ß4Î»*/
	RC_CtrlData.rc.LeftY = ((uint16_t)pData[4] >> 1 | (uint16_t)pData[5] << 7) & 0x07FF;
	
	RC_CtrlData.rc.RightX = (RC_CtrlData.rc.RightX - 0x400) / (0x694-0x400);
	RC_CtrlData.rc.RightY = (RC_CtrlData.rc.RightY - 0x400) / (0x694-0x400);
	RC_CtrlData.rc.LeftX = (RC_CtrlData.rc.LeftX - 0x400) / (0x694-0x400);
	RC_CtrlData.rc.LeftY = (RC_CtrlData.rc.LeftY - 0x400) / (0x694-0x400);
	
	/*pData[5]µÄ¸ß2Î»ÎªLeftSwitch*/
	RC_CtrlData.rc.LeftSwitch  = ((pData[5] >> 6) & 0x03);

	/*pData[6]µÄ6£¬7Î»ÎªRightSwitch*/
	RC_CtrlData.rc.RightSwitch  = ((pData[5] >> 4) & 0x03);

	/*pData[6],pData[7]Îªx*/
	RC_CtrlData.mouse.x    = ((int16_t)pData[6] | (int16_t)pData[7] << 8);

	/*pData[8],pData[9]Îªy*/
	RC_CtrlData.mouse.y    = ((int16_t)pData[8] | (int16_t)pData[9] << 8);

	/*pData[10],pData[11]Îªz*/
	RC_CtrlData.mouse.z    = ((int16_t)pData[10] | (int16_t)pData[11] << 8);

	/*pData[12]Îª×ó¼ü*/
	RC_CtrlData.mouse.press_l = pData[12];

	/*pData[13]ÎªÓÒ¼ü*/
	RC_CtrlData.mouse.press_r = pData[13];

	/*pData[14],pData[15]Îª¼üÅÌÖµ*/
	RC_CtrlData.key.v      = ((int16_t)pData[14]) | ((int16_t)pData[15] << 8);

}

